package com.keosimage.common;

import org.apache.log4j.Logger;

import weibo4j.org.json.JSONException;
import weibo4j.org.json.JSONObject;
import weibo4j.util.URLEncodeUtils;

public class KeosImageUtils {

	static Logger log = Logger.getLogger(KeosImageUtils.class.getName());

	public static boolean isEmpty(Object o) {
		if (o == null) {
			return true;
		}

		if (o instanceof String) {
			return "".equals(o);
		}
		return false;
	}

	public static boolean isSuccess(JSONObject jo)
			throws KeosImageWeiboOperationException {
		String error_code = null;
		String error = null;
		try {
			error_code = jo.getString("error_code");
			error = jo.getString("error");
		} catch (JSONException e) {
			log.error(e.getMessage());
			log.error(e.getCause());
		}
		if (!isEmpty(error_code)) {
			throw new KeosImageWeiboOperationException(error_code, error);
		}
		return true;
	}

	public static String addNullParam(Object o) {
		if (o == null) {
			return "";
		}
		return o.toString();
	}

	public static String addURLEncodeParam(String s) {
		if (!URLEncodeUtils.isURLEncoded(s)) {
			return URLEncodeUtils.encodeURL(s);
		}
		return s;
	}
}
